package com.cn.book;

import com.cn.book.config.MyConsulServerList;
import com.cn.book.services.Hello1Service;
import com.cn.book.services.Hello2Service;
import com.ecwid.consul.v1.ConsulClient;
import com.netflix.client.config.DefaultClientConfigImpl;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ServerList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableFeignClients
@RestController
public class ConsulConsumerDiscoveryClientNewApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsulConsumerDiscoveryClientNewApplication.class, args);
    }

    @Autowired
    private Hello1Service hello1Service;
    @Autowired
    private Hello2Service hello2Service;

    @RequestMapping(path = "/hello1")
    public String hello1(String name) {
        return hello1Service.sayHello(name);
    }

    @RequestMapping(path = "/hello2")
    public String hello2(String name) {
        return hello2Service.sayHello(name);
    }

    @Bean
    public ServerList<?> ribbonServerList(ConsulClient client, ConsulDiscoveryProperties properties) {
        MyConsulServerList serverList = new MyConsulServerList(client, properties);
        IClientConfig config = new DefaultClientConfigImpl();
        serverList.initWithNiwsConfig(config);
        return serverList;
    }
}
